<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

       <el-col :lg="4" :md="8">
            <el-form-item label="搜索:">
              <el-input
                clearable
                v-model="where.keywords"
                placeholder="请输入姓名或学籍号"/>
            </el-form-item>
          </el-col>
       <el-col :lg="4" :md="8">
         <el-form-item label="统计学年:">
           <el-select
             filterable
             clearable
             v-model="where.annual"
             @change="$forceUpdate()"
             size="small"
             placeholder="-学年统计-"
             class="ele-block">
             <el-option v-for="item in xuenianList" :key="item.id" :label="item.name" :value="item.id"/>
           </el-select>
         </el-form-item>
       </el-col>

       <el-col :lg="8" :md="10">
         <el-form-item label="统计日期:">
           <el-col :lg="8" :md="10">
             <el-date-picker
               type="date"
               class="ele-fluid"
               v-model="where.startTime"
               value-format="yyyy-MM-dd"
               format="yyyy-MM-dd"
               placeholder="请选择开始日期"/>
           </el-col>
           <el-col :lg="2" :md="12">
             至
           </el-col>
           <el-col :lg="8" :md="12">
             <el-date-picker
               type="date"
               class="ele-fluid"
               v-model="where.endTime"
               value-format="yyyy-MM-dd"
               format="yyyy-MM-dd"
               placeholder="请选择结束日期"/>
           </el-col>
         </el-form-item>
       </el-col>

       <el-col :lg="4" :md="8">
         <el-form-item label="项目:">
            <project-select  v-model="where.projectId" @chageProject="chageProject" @chageProjectId="chageProjectId"></project-select>
         </el-form-item>
       </el-col>

       <el-col :lg="4" :md="8">
         <el-form-item ref="grade" prop="grade" label="年级:">
           <grade-select  v-model="where.gradeId"  @changeGrade="changeGrade" @changeGradeId="changeGradeId"></grade-select>
         </el-form-item>
       </el-col>
       <el-col :lg="4" :md="8">
         <el-form-item label="院系:">
           <el-select
             filterable
             clearable
             v-model="where.departmentsId"
             @change="$forceUpdate();getProfessionalList();getClassList();"
             size="small"
             placeholder="-请选择院系-"
             class="ele-block">
             <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
           </el-select>
         </el-form-item>
       </el-col>
       <el-col :lg="4" :md="8">
         <el-form-item label="专业:" prop="professional">
             <el-select
               clearable
               v-model="where.professionalId"
               @change="$forceUpdate();getClassList();"
               size="small"
               placeholder="-请选择专业-"
               class="ele-block">
               <el-option v-for="item in professionalList" :key="item.id" :label="item.name" :value="item.id"/>
             </el-select>
         </el-form-item>
       </el-col>
       <el-col :lg="4" :md="8">
         <el-form-item label="班级:" prop="professional">
             <el-select
               clearable
               v-model="where.classId"
               @change="$forceUpdate()"
               size="small"
               placeholder="-请选择班级-"
               class="ele-block">
               <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"/>
             </el-select>
         </el-form-item>
       </el-col>
     <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        v-if="showtable"
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
 <template slot="toolbar">
                    <el-button
                      @click="exportData"
                      type="primary"
                      icon="el-icon-download"
                      class="ele-btn-icon"
                      size="small">导出
                    </el-button>
                  </template>
                            <!-- 用户头像列 -->
          <template slot="avatar" slot-scope="{row}">
            <el-avatar shape="square" :size="25" :src="row.avatar"/>
          </template>
          <!-- 账号类型列 -->
          <template slot="type" slot-scope="{row}">
                      <el-tag v-if="row.type === 1" type="" size="small">=学生</el-tag>
                    </template>

  <template slot="gender" slot-scope="{row}">
          <el-tag
            :type="['success', 'primary', 'warning'][row.gender-1]"
            size="mini">
            {{ ['男', '女', '保密'][row.gender - 1] }}
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            @click="openAnalysis(row)"
          >学生画像
          </el-link>
        </template>


                        </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <memberstu-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import MemberstuEdit from './memberstu-edit';
import gradeSelect from '@/components/gradeSelect'
import projectSelect from '@/components/projectSelect'
import XLSX from 'xlsx';

export default {
  name: 'ExamMemberstu',
  components: {MemberstuEdit,gradeSelect,projectSelect},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      showtable:false,
      // 表格数据接口
      url: '/memberstu/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
          {
                   prop: 'pro_name',
                   label: '项目',
                   minWidth: 200,
                   align: 'center',
                 },
                 {
                   prop: 'realname',
                   label: '姓名',
                   showOverflowTooltip: true,
                   minWidth: 200,
                   align: 'center',
                 },
                                   {
                   prop: 'roll',
                   label: '学籍号',
                   showOverflowTooltip: true,
                   minWidth: 200,
                   align: 'center',
                 },
          {
                   prop: 'grade_name',
                   label: '年级',
                   showOverflowTooltip: true,
                   width: 100,
                   align: 'center',

                 },
                 {
                   prop: 'prof_name',
                   label: '专业',
                   showOverflowTooltip: true,
                   minWidth: 200,
                   align: 'center',
                 },
               {
                   prop: 'class_name',
                   label: '专业班级',
                   showOverflowTooltip: true,
                   minWidth: 200,
                   align: 'center',
                 },

                             {
                   prop: 'gender',
                   label: '性别',
                   showOverflowTooltip: true,
                   minWidth: 200,
                   align: 'center',
                   slot: 'gender'
                 },
                 {
                    prop: 'avg',
                    label: '平均分',
                    showOverflowTooltip: true,
                    minWidth: 200,
                    align: 'center',
                  },
                  {
                    prop: 'youRate',
                    label: '优良率',
                    showOverflowTooltip: true,
                    minWidth: 200,
                    align: 'center',
                  },
                  {
                    prop: 'hegeRate',
                    label: '合格率',
                    showOverflowTooltip: true,
                    minWidth: 200,
                    align: 'center',
                  },
                {
                  columnKey: 'action',
                  label: '操作',
                  width: 150,
                  align: 'center',
                  resizable: false,
                  slot: 'action',
                  fixed: "right"
                }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      projectList:[],
      xuenianList:[],
      classList: [],
      gradeList:[],
      departmentsList:[],
      professionalList:[],
    };
  },
  created() {
	  this.where.departmentsId = this.$route.query.departmentsId?Number(this.$route.query.departmentsId):null
	  this.where.professionalId = this.$route.query.professionalId?Number(this.$route.query.professionalId):null
	  this.where.classId = this.$route.query.id?Number(this.$route.query.id):null
    // 获取项目
    this.getProjectList();
    // 获取学年
    this.getXueList();
    this.getDepartmentsList();
  },
  methods: {
    changeGrade(val){
      this.where.gradeId = val;
      if(this.where.projectId&&this.where.gradeId){
        this.showtable = true
      }
      // 获取班级列表
      this.getClassList();
    },
    chageProject(val){
      this.where.projectId = val;
      if(this.where.projectId&&this.where.gradeId){
        this.showtable = true
      }
    },
    chageProjectId(val){
      this.where.projectId = val;
      this.$forceUpdate();
    },
    changeGradeId(val){
      this.where.gradeId = val;
      this.$forceUpdate();
      // 获取班级列表
      this.getClassList();
    },
    openAnalysis(row){
      this.$router.push({path: '/evaluate/studentAnalysis', query: {name: row.realname,id:row.id}});
    },
    /* 导出数据 */
    exportData() {
      let array = [['ID', '项目', '姓名', '学籍号', '年级','专业','专业班级','性别', '平均分','优良率','合格率']];
      const loading = this.$loading({lock: true});
      this.$http.get('/memberstu/index?page=1&limit=999999&projectId='+this.where.projectId+'&gradeId='+this.where.gradeId+'&annual='+this.where.annual+'&startTime='+this.where.startTime+'&endTime='+this.where.endTime+'&departmentsId='+this.where.departmentsId+'&professionalId='+this.where.professionalId+'&classId='+this.where.classId+'&gender='+this.where.gender+'&keywords='+this.where.keywords).then(res => {
        loading.close();
        if (res.data.code === 0) {
          res.data.data.forEach(d => {
            array.push([
              d.id,
              d.pro_name,
              d.realname,
              d.roll,
              d.grade_name,
              d.prof_name,
              d.class_name,
              d.gender_str,
              d.avg,
              d.youRate,
              d.hegeRate,
             // this.$util.toDateString(d.createTime)
            ]);
          });
          this.$util.exportSheet(XLSX, array, '学生概览');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
   /**
     * 获取专业列表
     */
    getProfessionalList() {
      if(this.where.professional){
        this.$forceUpdate();
      }
      this.$http.post('/professional/getProfessionalList',{sid:this.where.departmentsId}).then(res => {
        if (res.data.code === 0) {
          this.professionalList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
     this.getClassList();
    },
    /**
     * 获取院系列表
     */
     getDepartmentsList() {
      this.$http.post('/departments/getDepartmentsList').then(res => {
         if (res.data.code === 0) {
           this.departmentsList = res.data.data;
         } else {
           this.$message.error(res.data.msg);
         }
       }).catch(e => {
         this.$message.error(e.message);
       });
      this.getProfessionalList();
      this.getClassList();
   },
   /**
       * 获取组织列表
       */
      getProjectList() {
        this.$http.get('/project/getProjectList').then(res => {
          if (res.data.code === 0) {
            this.projectList = res.data.data;
            this.where.projectId=this.projectList && this.projectList.length > 0 ? this.projectList[0].id : ''
            return this.where.projectId
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.$message.error(e.message);
        });
      },
      getXueList() {
        this.$http.get('/annual/getXueList').then(res => {
          if (res.data.code === 0) {
            this.xuenianList = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.$message.error(e.message);
        });
      },
   /**
       * 获取班级列表
       */
      getClassList() {
        if(this.where.superior){
          this.where.class = null;
          this.$forceUpdate();
        }
        this.$http.post('/clss/getClassList',{yid:this.where.departmentsId,gid:this.where.gradeId,pid:this.where.professionalId}).then(res => {
          if (res.data.code === 0) {
            this.classList = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.$message.error(e.message);
        });
      },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {
        projectId:this.where.projectId,
        gradeId:this.where.gradeId
      };
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/memberstu/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的学生概览吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/memberstu/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
                                                                                                                                                                                                                            }
}
</script>

<style scoped>
</style>
